package com.yyfax.ph.products;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.network.entity.EmptyEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

import java.util.ArrayList;
import java.util.List;

public class ProductsModel extends Model {
	private static final String TAG = ProductsModel.class.getSimpleName();

	@Override
	public void onNext(Object obj) {
		ProductsResult detailResult = cast(obj);
		EventAgent.post(detailResult);
	}

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		return service.getDetail(token, new EmptyEntity());
	}

	private interface Service {
		@POST("sys/product/list")
		Observable<ProductsResult> getDetail(@Header("token") String token, @Body EmptyEntity params);
	}


	public static class ProductsResult extends BaseResultEntity<Products> {
	}


	public static class Products {
		List<Product> hot = new ArrayList<>();
		List<Product> more = new ArrayList<>();
	}

	protected static class Product {
		/** 产品详情Url */
		public String productUrl = "";
		/** 产品名称 */
		public String name = "";
		/** 产品logo */
		public String imageUrl = "";
		/** 产品简介 */
		public String description = "";

		@Override
		public String toString() {
			final StringBuffer sb = new StringBuffer("Hot{");
			sb.append("id='").append(productUrl).append('\'');
			sb.append(", name='").append(name).append('\'');
			sb.append(", imageUrl='").append(imageUrl).append('\'');
			sb.append(", description='").append(description).append('\'');
			sb.append('}');
			return sb.toString();
		}
	}

}
